import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../resource/language/generated/l10n.dart';
import '../../extensions/channel_extension.dart';
import '../../interface/room_page_model.dart';

/// 兜风特权描述
class PrivilegeDescWidget extends StatefulWidget {
  const PrivilegeDescWidget({Key? key}) : super(key: key);

  @override
  State<PrivilegeDescWidget> createState() => _PrivilegeDescWidgetState();
}

class _PrivilegeDescWidgetState extends State<PrivilegeDescWidget> {
  late RoomPageModel roomPageModel;
  var _expanded = false;
  var _canExpand = false;

  @override
  void initState() {
    super.initState();
    roomPageModel = context.read<RoomPageModel>();
  }

  // bool get isBoss =>
  //     roomPageModel.roomModel.meWithSlot?.micType == MicTypeEnum.CAR_OWNER_MIC;

  bool get isBoss => true;

  Widget get title => Center(
        child: ShaderMask(
          shaderCallback: (bounds) => const LinearGradient(
            colors: <Color>[
              Color(0xFFFAD1B3),
              Color(0xFFFFEDC6),
            ],
          ).createShader(bounds),
          child: Text(
            T.current!.df_privilege,
            style: const TextStyle(
                fontSize: 18, fontWeight: FontWeight.w500, color: Colors.white),
          ),
        ),
      );

  Widget get endIcon => Align(
        alignment: Alignment.centerRight,
        child: Padding(
          padding: const EdgeInsets.only(right: 23),
          child: CupertinoButton(
            minSize: 0,
            padding: EdgeInsets.zero,
            onPressed: () async {
              // Reporter.track(TrackEvent.ridePrivilege, {
              //   "rid": roomPageModel.roomModel.channel!.channelId.toInt(),
              //   "step": "close",
              // });

              // var endPrivilege = await AutoRideApi.instance.endPrivilege();
              // if (!endPrivilege.isSuccess) showToast(endPrivilege.errorMsg!);
            },
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Image.asset(
                  "assets/images/ic_operate_end.png",
                  width: 12,
                  height: 12,
                  fit: BoxFit.contain,
                ),
                const SizedBox(
                  width: 5,
                ),
                // if (UserManager.instance.currentUser!.job == Job.BOSS)
                Text(
                  T.current.ending,
                  style: const TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w400,
                    color: Color(0xff999999),
                  ),
                ),
              ],
            ),
          ),
        ),
      );

  Widget get header => Container(
        height: 26,
        margin: const EdgeInsets.only(top: 14, bottom: 9),
        child: Stack(
          children: [title, if (isBoss) endIcon],
        ),
      );

  // CarMetaInfo? get carInfo => roomPageModel.channel?.carInfo;

  String get descStr {
    return 'descStr';
    // var desStrList = carInfo?.privilegeExtDesc.split('\n');

    // return !_expanded
    //     ? desStrList?.first ?? ''
    //     : (carInfo?.privilegeExtDesc ?? '');
  }

  Widget get content => Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: const Color(0xFF757575).withAlpha((0.1 * 255).toInt()),
        ),
        padding: const EdgeInsets.only(top: 13, left: 15.5, right: 15.5),
        margin: const EdgeInsets.symmetric(horizontal: 12),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              'privilegeDesc',
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.w400,
                color: Color(0xFFD5C3B2),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 8),
              child: Text(
                descStr,
                style: const TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w400,
                  color: Colors.white,
                ),
              ),
            )
          ],
        ),
      );

  @override
  Widget build(BuildContext context) {
    // _canExpand = carInfo?.privilegeExtDesc.contains('\n') ?? false;

    return Container(
      margin: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        gradient: const LinearGradient(
            colors: [Color(0xFF1F1C22), Color(0xFF2D292F)]),
        borderRadius: BorderRadius.circular(16),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          header,
          content,
          GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              _expanded = !_expanded;
              setState(() {});
            },
            child: Container(
              height: 30,
              alignment: Alignment.center,
              child: _canExpand
                  ? Image.asset(
                      _expanded
                          ? 'assets/images/ic_privilege_desc_collapse.webp'
                          : 'assets/images/ic_privilege_desc_expand.webp',
                      width: 15,
                      height: 14,
                    )
                  : const SizedBox(),
            ),
          ),
        ],
      ),
    );
  }
}
